package org.madmac.login;

import org.madmac.exception.LoginFailedException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.madmac.modulo.EModulo;

import org.madmac.usuario.Usuario;
import org.wakeful.conn.Connection;

public class Login {

   /**
    *
    * @param user
    * @param passwd - já criptografada!
    * @return
    * @throws SQLException
    * @throws ClassNotFoundException
    * @throws LoginFailedException
    */
   public Usuario iniciarSessao(String user, String passwd)
           throws SQLException, ClassNotFoundException, LoginFailedException, Exception {

//        String sql = "SELECT uu.code_usuario, uu.nickname, mm.descricao AS modulo ";
//        sql += "FROM unify.usuarios uu, madmac.usuarios mu, madmac.modulos mm	";
//        sql += "WHERE mu.code_usuario = uu.code_usuario ";
//		  sql += "AND mu.modulo = mm.code_modulo ";
//        sql += "AND uu.login = '" + user + "' ";
//        sql += "AND uu.senha = md5('" + passwd + "') ";
      Usuario usuario = null;
      java.sql.Connection conn = Connection.getInstance();
      PreparedStatement pstmt = conn.prepareStatement(LoginQuery.validarUsuario());
      pstmt.setString(1, user);
      pstmt.setString(2, passwd);

      ResultSet rs = null;
      try {
         System.out.println("Query: " + LoginQuery.validarUsuario()); // just for debug
         rs = pstmt.executeQuery();

         if (rs.next()) {
            usuario = new Usuario();
            usuario.setIdUsuario(rs.getInt("id_usuario"));
            usuario.setNickname(rs.getString("nickname"));
            usuario.setModulo(EModulo.valueOf(rs.getString("modulo")));
         } else {
            throw new LoginFailedException("Login ou Senha Incorretos.");
         }
      } catch (Exception e) {
//         Conn.restartConnection();
         return null;
      } finally {
         pstmt.close();
         if (rs != null) {
            rs.close();
         }
      }
      return usuario;
   }

   /**
    * IMPLEMENTAR MAIS TARDE
    * @param code_usuario
    * @return
    * @throws SQLException
    * @throws ClassNotFoundException
    * @deprecated
    */
   public boolean ativarSessao(int code_usuario) throws SQLException, ClassNotFoundException, Exception {

      java.sql.Connection conn = Connection.getInstance();
      Statement stmt = conn.createStatement();
      String sql = "UPDATE unify.usuarios SET sessao_ativa = 'true' ";
      sql += "WHERE code_usuario = " + code_usuario + " ";

      int result = stmt.executeUpdate(sql);

      if (result == 1) {
         return true;
      } else {
         return false;
      }
   }

   /**
    * IMPLEMENTAR MAIS TARDE
    * @param code_usuario
    * @return
    * @throws SQLException
    * @throws ClassNotFoundException
    * @deprecated
    */
   public boolean desativarSessao(int code_usuario) throws SQLException, ClassNotFoundException, Exception {

      java.sql.Connection conn = Connection.getInstance();
      Statement stmt = conn.createStatement();
      String sql = "UPDATE unify.usuarios SET sessao_ativa = 'false' ";
      sql += "WHERE code_usuario = " + code_usuario + " ";

      int result = stmt.executeUpdate(sql);

      if (result == 1) {
         return true;
      } else {
         return false;
      }
   }
}
